
/* JavaScript content from map/classes/Car.js in folder common */


define(['dojo', 'map/classes/Utils'], function(dojo, Utils ){

  var HELI_ID = 99999;   


  return dojo.declare('map.classes.Car', [], {
    carProps : {
      heading: 0
    },
    debug: false,
    rate :  500,
    tickByFPS : function(tick){

      return ( tick * Math.floor( 888 / window.renderSettings.fps ) ); 
    },    
    currentStatus : {
      emergency: 0,
      rain: 0,
      accident: 0
    },
    messageSent : {
      emergency: {
        warning: false,
        danger: false
      },
      rain: false,
      accident: false
    },  
    pos : {
      x: null,
      y: null
    },
    lastUpdateGeo : {
      lon: null,
      lat: null
    },
    lastGeo : {
      lon: null,
      lat: null
    },
    geo : {
      lon: null,
      lat: null
    },
    vector : {
      x: 0,
      y: 0
    },
    dispVector : {
      x: 0,
      y: 0
    },
    state : 0,
    lastUpdateTime : null,
    lastPointTime : null,
    changedZoom : false,
    queue : [],   // queue of positions to move to, on one-second intervals
    color : "rgba(130, 250, 250,0.75)",    
    scale : 1.5,
    factor : 1.0,
    messages : [],  // list of msgData objects, for displaying a list of received messages for a particular car
    _confirmed : false,
    constructor:function(id, geo, canvas, map){
      this.id = id;
      this.geo = geo;
      this.canvas = canvas;
      this.map = map;

    },
    _getGeoString : function() {
      return "(" + (this.geo.lon).toFixed(5) + ", " + (this.geo.lat).toFixed(5) + ")";
    },
    _getRadians : function() {
      var rad = Utils.vectorToRadians(this.vector);
      if (this.vector.x < 0) {
        rad += Math.PI;
      }
      return rad;
    },
    _getDegrees : function() {
      return this._getRadians() * 360 / (2 * Math.PI);
    },

    _getHeading : function() {
      return this.carProps ? this.carProps.heading : 0;
      //return me._getDegrees() + 90;
    },     
    checkTirePressure: function () {
      if (this.carProps.tirePressure === 0) {
        this.currentStatus.accident = 2;
        if (!this._flashing) {
          this._flashing = true;
          // flash(this.id);
        }
        this.colorIndex = 2;
      } else {
        if (this.currentStatus.accident == 2) {
          this.currentStatus.accident = 1;
        }
        this._flashing = false;
        this.colorIndex = 0;
      }
    },
    getDisplayType: function(zoom){        
      if (zoom <= 16) {
        return "circle";
      } else if (this.id % 4 === 0 || this.id % 4 === 1 || this.id % 4 === 2) {
        return "car";
      } else {
        return "truck";
      }
    },
    getXYDist : function(pt1, pt2) {
      var dist = Math.sqrt(Math.pow(pt2.x - pt1.x, 2) + Math.pow(pt2.y - pt1.y, 2));
      return dist;
    },
    getGeoDist: function(geo1, geo2) {
      var dist = Math.sqrt(Math.pow(geo2.lon - geo1.lon, 2) + Math.pow(geo2.lat - geo1.lat, 2));
      return dist;
    },
    interpCoords : function(start, end, delta, t){
      t = t || this.tickByFPS(this.rate);      

      var mid = {
        lon : (start.lon * (delta / t)) + (end.lon * (t - delta) / t),
        lat : (start.lat * (delta / t)) + (end.lat * (t - delta) / t)
      };

      return mid;
    },
    interp : function(start, end, i){

      while(i--){
        this.interpCoords(start, end);
      }

    },  
    dupCount :0 ,
    update : function() {
      var lat = this.geo.lat
        , lon = this.geo.lon;

      if(this.lastUpdateGeo.lat === lat && this.lastUpdateGeo.lon === lon){ 
        this.dupCount++;        
        
        if(this.debug) console.log(this.rate, this.dupCount);

        if(this.nextGeo && this.interpLocation && ( this.dupCount  < this.rate ) ) {
          this.geo = this.interpCoords( this.lastGeo, this.nextGeo , this.rate - this.dupCount  ,  this.rate );
        }

        if(this.dupCount > window.tickSettings.maxRate && (this.debug) ){
          console.log("I'm dead, Jim!", this.dupCount);
        }

      }

      this.lastUpdateGeo = this.geo;

    },
    addToQueue : function(pos) {      
      
      var queueObj = pos;      
      this.queue.push(queueObj);

    },  
    ptDistance : function (start, end) {
      var dist = Math.sqrt(Math.pow(start.lon - end.lon, 2) + Math.pow(start.lat - end.lat, 2));
      return dist;
    },

    updatePosition: function(data) {
      this.interpLocation = true;

      if(this.geo.lat === null || this.geo.lon  === null ){
        this.nextGeo = this.lastGeo = this.geo = { lat : data.lat, lon : data.lon };
        this.interpLocation = false;  
        return;
      }

      // if( (this.geo.lat === data.lat && this.geo.lon === data.lon) ||  ( this.nextGeo && (this.nextGeo.lat === data.lat && this.nextGeo.lon === data.lon )  ) ) return;
      
      this.updateTickrate();

      this.lastGeo = this.geo;
      this.nextGeo = { lat : data.lat, lon : data.lon };
      this.lastUpdateGeo = this.geo;

      if( this.ptDistance(this.geo, this.nextGeo) > 0.0005 ){
        this.geo = this.lastGeo = this.nextGeo; 
        this.interpLocation = false;       
      } 

      this.vector = 1;
      this.carProps = data.carProps;

    },    
    updateTickrate : function(){

      if(window.tickSettings.force){
        this.rate = window.tickSettings.forcedRate;
      }
      else {
        
        var newRate = ( this.rate +  this.dupCount ) / 2;

        if( newRate <= window.tickSettings.maxRate && newRate > window.tickSettings.minRate){
          this.rate = newRate;
        }else {
          this.rate = (newRate < window.tickSettings.minRate) ? window.tickSettings.minRate : window.tickSettings.maxRate;
        }
      }
         
      window.tickSettings.averageRate = (this.rate +  window.tickSettings.averageRate) / 2;

      this.dupCount = 0;
    }


  }); 
});